package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleServices;
import com.sun.istack.internal.NotNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * date: 2024/09/16 18:48<br/>
 * Description:帖子回复接口
 *
 * @author: jmy <br/>
 */
@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleServices articleServices;
    @Resource
    private IArticleReplyService articleReplyService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("帖子id") @NotNull @RequestParam("articleId") Long articleId,
                            @ApiParam("回复内容") @NotNull @RequestParam("content") String content) {
        //获取用户信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        //判断用户状态是否异常
        if (user.getState() == 1) {
            log.warn(AppResult.failed(ResultCode.FAILED_USER_BANNED).getMessage());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        //判断帖子状态是否异常
        Article article = articleServices.selectById(articleId);
        if (article == null) {
            log.warn(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS).getMessage());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (article.getState() == 1) {
            log.warn(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED).getMessage());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED);
        }

        //构造回复数据
        ArticleReply reply = new ArticleReply();
        reply.setArticleId(articleId);//要回复的帖子id
        reply.setContent(content);//回复内容
        reply.setPostUserId(user.getId());//回复人id
        //插入数据
        Integer result = articleReplyService.create(reply);
        if (result == 0) {
            log.warn(AppResult.failed(ResultCode.FAILED).getMessage());
            return AppResult.failed(ResultCode.FAILED);
        }
        return AppResult.success(result);
    }


    //获取回复列表
    @GetMapping("/getReplies")
    @ApiOperation("获取回复列表")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("文章id") @NotNull @RequestParam("articleId") Long articleId) {
        //参数校验
        if (articleId <= 0) {
            log.warn(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS).getMessage());
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        //调用service获取结果
        List<ArticleReply> replies = articleReplyService.selectByArticleId(articleId);
        return AppResult.success(replies);
    }
}
